using Microsoft.Phone.Controls.Primitives;
using System;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Microsoft.Phone.Controls
{
	[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(MenuItem)), TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates"), TemplateVisualState(Name = "Disabled", GroupName = "CommonStates"), TemplateVisualState(Name = "Normal", GroupName = "CommonStates"), TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
	public class MenuItem : HeaderedItemsControl
	{
		private bool _isFocused;
		public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MenuItem), new PropertyMetadata(null, new PropertyChangedCallback(MenuItem.OnCommandChanged)));
		public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(MenuItem), new PropertyMetadata(null, new PropertyChangedCallback(MenuItem.OnCommandParameterChanged)));
		public event RoutedEventHandler Click;
		internal MenuBase ParentMenuBase
		{
			get;
			set;
		}
		public ICommand Command
		{
			get
			{
				return (ICommand)base.GetValue(MenuItem.CommandProperty);
			}
			set
			{
				base.SetValue(MenuItem.CommandProperty, value);
			}
		}
		public object CommandParameter
		{
			get
			{
				return base.GetValue(MenuItem.CommandParameterProperty);
			}
			set
			{
				base.SetValue(MenuItem.CommandParameterProperty, value);
			}
		}
		private static void OnCommandChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((MenuItem)o).OnCommandChanged((ICommand)e.get_OldValue(), (ICommand)e.get_NewValue());
		}
		private void OnCommandChanged(ICommand oldValue, ICommand newValue)
		{
			if (oldValue != null)
			{
				oldValue.remove_CanExecuteChanged(new EventHandler(this.OnCanExecuteChanged));
			}
			if (newValue != null)
			{
				newValue.add_CanExecuteChanged(new EventHandler(this.OnCanExecuteChanged));
			}
			this.UpdateIsEnabled(true);
		}
		private static void OnCommandParameterChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((MenuItem)o).OnCommandParameterChanged();
		}
		private void OnCommandParameterChanged()
		{
			this.UpdateIsEnabled(true);
		}
		public MenuItem()
		{
			base.set_DefaultStyleKey(typeof(MenuItem));
			base.add_IsEnabledChanged(new DependencyPropertyChangedEventHandler(this.OnIsEnabledChanged));
			base.SetValue(TiltEffect.IsTiltEnabledProperty, true);
			base.add_Loaded(new RoutedEventHandler(this.OnLoaded));
			this.UpdateIsEnabled(false);
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.ChangeVisualState(false);
		}
		protected override void OnGotFocus(RoutedEventArgs e)
		{
			base.OnGotFocus(e);
			this._isFocused = true;
			this.ChangeVisualState(true);
		}
		protected override void OnLostFocus(RoutedEventArgs e)
		{
			base.OnLostFocus(e);
			this._isFocused = false;
			this.ChangeVisualState(true);
		}
		protected override void OnMouseEnter(MouseEventArgs e)
		{
			base.OnMouseEnter(e);
			base.Focus();
			this.ChangeVisualState(true);
		}
		protected override void OnMouseLeave(MouseEventArgs e)
		{
			base.OnMouseLeave(e);
			if (this.ParentMenuBase != null)
			{
				this.ParentMenuBase.Focus();
			}
			this.ChangeVisualState(true);
		}
		protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			if (!e.get_Handled())
			{
				this.OnClick();
				e.set_Handled(true);
			}
			base.OnMouseLeftButtonUp(e);
		}
		protected override void OnKeyDown(KeyEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			if (!e.get_Handled() && 3 == e.get_Key())
			{
				this.OnClick();
				e.set_Handled(true);
			}
			base.OnKeyDown(e);
		}
		protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
		{
			throw new NotImplementedException();
		}
		protected virtual void OnClick()
		{
			ContextMenu contextMenu = this.ParentMenuBase as ContextMenu;
			if (contextMenu != null)
			{
				contextMenu.ChildMenuItemClicked();
			}
			RoutedEventHandler click = this.Click;
			if (click != null)
			{
				click.Invoke(this, new RoutedEventArgs());
			}
			if (this.Command != null && this.Command.CanExecute(this.CommandParameter))
			{
				this.Command.Execute(this.CommandParameter);
			}
		}
		private void OnCanExecuteChanged(object sender, EventArgs e)
		{
			this.UpdateIsEnabled(true);
		}
		private void UpdateIsEnabled(bool changeVisualState)
		{
			base.set_IsEnabled(this.Command == null || this.Command.CanExecute(this.CommandParameter));
			if (changeVisualState)
			{
				this.ChangeVisualState(true);
			}
		}
		private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			this.ChangeVisualState(true);
		}
		private void OnLoaded(object sender, RoutedEventArgs e)
		{
			this.ChangeVisualState(false);
		}
		protected virtual void ChangeVisualState(bool useTransitions)
		{
			if (!base.get_IsEnabled())
			{
				VisualStateManager.GoToState(this, "Disabled", useTransitions);
			}
			else
			{
				VisualStateManager.GoToState(this, "Normal", useTransitions);
			}
			if (this._isFocused && base.get_IsEnabled())
			{
				VisualStateManager.GoToState(this, "Focused", useTransitions);
				return;
			}
			VisualStateManager.GoToState(this, "Unfocused", useTransitions);
		}
	}
}
